package org.yuanqiframework.yuanqi.boot.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.core.utils.StringUtils;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.core.utils.BeanUtils;
import org.yuanqiframework.yuanqi.boot.sys.dto.SysDictDTO;
import org.yuanqiframework.yuanqi.boot.sys.dto.SysDictOptionDTO;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysDict;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysDictOption;
import org.yuanqiframework.yuanqi.boot.sys.service.SysDictOptionService;
import org.yuanqiframework.yuanqi.boot.sys.service.SysDictService;
import org.yuanqiframework.yuanqi.boot.sys.vo.SysDictTreeOptionVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/dict")
public class DictController extends BaseController {
    @Resource
    private SysDictService sysDictService;
    @Resource
    private SysDictOptionService sysDictOptionService;


    @GetMapping(value = "queryPage")
    public AjaxResult<IPage<SysDict>> queryPage(@RequestParam(required = false) String name,
                                                @RequestParam(required = false) String code,
                                                @RequestParam(required = false) String type,
                                                @RequestParam(required = false) String status,
                                                Integer pageNum, Integer pageSize) {
        return AjaxResult.success(sysDictService.page(new Page<SysDict>(pageNum, pageSize), new LambdaQueryWrapper<SysDict>().eq(StringUtils.isNotEmpty(name), SysDict::getName, name).eq(StringUtils.isNotEmpty(code), SysDict::getCode, code).eq(StringUtils.isNotEmpty(type), SysDict::getType, type).eq(StringUtils.isNotEmpty(status), SysDict::getStatus, status).orderByAsc(SysDict::getType).orderByAsc(SysDict::getId)));
    }

    @GetMapping(value = "query/{code}")
    public AjaxResult<?> query(@PathVariable String code) {
        String type = sysDictService.queryTypeByCode(code);
        if (StringUtils.isEmpty(type)) {
            return AjaxResult.error("未找到字典类型");
        }
        if ("list".equals(type)) {
            return AjaxResult.success(sysDictService.queryListByCode(code));
        } else if ("tree".equals(type)) {
            return AjaxResult.success(sysDictService.queryTreeByCode(code));
        }
        return AjaxResult.error("未找到字典");
    }

    @GetMapping(value = "queryListData/{id}")
    public AjaxResult<List<SysDictOption>> queryListData(@PathVariable Long id) {
        return AjaxResult.success(sysDictService.queryListData(id));
    }


    @GetMapping(value = "queryTreeData/{id}")
    public AjaxResult<List<SysDictTreeOptionVO>> queryTreeData(@PathVariable Long id) {
        return AjaxResult.success(sysDictService.queryTreeData(id));

    }


    @PostMapping(value = "create")
    public AjaxResult<?> create(@Valid @RequestBody SysDictDTO sysDictDTO) {
        return toAjax(sysDictService.create(sysDictDTO));
    }


    @PostMapping(value = "update/{id}")
    public AjaxResult<?> update(@PathVariable Long id, @Valid @RequestBody SysDictDTO sysDictDTO) {
        SysDict sysDict = new SysDict();
        BeanUtils.copyProperties(sysDictDTO, sysDict);
        sysDict.setId(id);
        return toAjax(sysDictService.updateById(sysDict));
    }


    @PostMapping(value = "delete/{id}")
    public AjaxResult<?> delete(@PathVariable String id) {
        return toAjax(sysDictService.delete(id));
    }


    @PostMapping(value = "option/create")
    public AjaxResult<?> createOption(@Valid @RequestBody SysDictOptionDTO sysDictDTO) {
        SysDictOption sysDictOption = new SysDictOption();
        BeanUtils.copyProperties(sysDictDTO, sysDictOption);
        return toAjax(sysDictOptionService.save(sysDictOption));
    }


    @PostMapping(value = "option/update/{id}")
    public AjaxResult<?> updateOption(@PathVariable Long id, @Valid @RequestBody SysDictOptionDTO sysDictDTO) {
        SysDictOption sysDictOption = new SysDictOption();
        BeanUtils.copyProperties(sysDictDTO, sysDictOption);
        sysDictOption.setId(id);
        return toAjax(sysDictOptionService.updateById(sysDictOption));
    }


    @PostMapping(value = "option/delete/{id}")
    public AjaxResult<?> deleteOption(@PathVariable Long id) {
        return toAjax(sysDictOptionService.removeById(id));
    }

}